﻿using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;

namespace Clothes.Masters.ModuleData
{
    #region RRDesctiptionType
    public class RRDesctiptionType : DataReaderUpdater
    {
        private DescriptionType type;
        private SqlABParameter sqlP1;

        public RRDesctiptionType(DescriptionType type)
            : base(false)
        {
            this.type = type;
        }

        protected override void CreateConnection()
        {
            myTable = CL_Descriptions.Name;

            QueryBuilder qb = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", CL_Descriptions.Type);

            qb.AddSelect();
            qb.AddFrom(CL_Descriptions.Name);
            qb.AddCompare(CL_Descriptions.Type, sqlP1);
            qb.AddOrder(CL_Descriptions.Description);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);

        }

        protected override void SetParameters()
        {
            sqlP1.Value = type;
        }
    }

    #endregion

    #region DRDesctiptionTypeByDesc
    public class DRDesctiptionTypeByDesc : DataReaderUpdater
    {
        private DescriptionType type;
        private string desc;
        private SqlABParameter sqlP1, sqlP2;

        public DRDesctiptionTypeByDesc(DescriptionType type)
            : base(false)
        {
            this.type = type;
        }

        protected override void CreateConnection()
        {
            myTable = CL_Descriptions.Name;

            QueryBuilder qb = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", CL_Descriptions.Type);
            sqlP2 = new SqlABParameter("@p52", CL_Descriptions.Description);

            qb.AddSelect();
            qb.AddFrom(CL_Descriptions.Name);
            qb.AddCompare(CL_Descriptions.Type, sqlP1);
            qb.AddCompare(CL_Descriptions.Description, sqlP2);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);

        }

        protected override void SetParameters()
        {
            sqlP1.Value = type;
            sqlP2.Value = desc;
        }

        public bool Find(string desc)
        {
            this.desc = desc;
            return base.Find();
        }
    }

    #endregion

    #region DRMastersByFiscalCode

    public class DRMastersByFiscalCode : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private string fiscalcode;

        public DRMastersByFiscalCode(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = CL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", CL_Masters.FiscalCode);

            qB.AddSelect();
            qB.AddFrom(CL_Masters.Name);
            qB.AddCompare(CL_Masters.FiscalCode, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = fiscalcode;
        }

        public bool Find(string fiscalCode)
        {
            this.fiscalcode = fiscalCode;
            return base.Find();
        }
    }

    #endregion

    #region DRMasters

    public class DRMasters : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private int id;

        public DRMasters(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = CL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", CL_Masters.ID);

            qB.AddSelect();
            qB.AddFrom(CL_Masters.Name);
            qB.AddCompare(CL_Masters.ID, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = id;
        }

        public bool Find(int id)
        {
            this.id = id;
            return base.Find();
        }
    }

    #endregion

}